<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Tdc extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('user','',TRUE);
		$this->load->model('myconfig','',TRUE);
		$this->load->model('category','',TRUE);
		$this->load->model('answer','',TRUE);
		$this->load->model('test','',TRUE);
		$this->load->model('question','',TRUE);
		$this->load->helper(array('form', 'url'));
	}

	function index()
	{
		if($this->session->userdata('logged_in'))
		{
			$session_data = $this->session->userdata('logged_in');
			$data['username'] = $session_data['username'];
			$result = $this->user->get_user($data['username']);
			if ($result)
			{
				$data['full_name'] = $result->full_name;
				$data['last_login'] = $result->last_login;
				$data['is_admin'] = $result->is_admin;
			}
			
			$config = $this->myconfig->get_config();
			
			$data['default_max_score'] = '';
			$data['default_time'] = '';
			$data['default_num_question'] = '';
			if ($config)
			{
				$data['default_max_score'] = $config->default_max_score;
				$data['default_time'] = $config->default_time;
				$data['default_num_question'] = $config->default_num_question;	
			}
			
			$data['total_question'] = $this->question->get_total_question();
			$data['today_question'] = $this->question->get_today_question();
			$data['total_test'] = $this->test->get_total_test();
			$data['today_test'] = $this->test->get_today_test();
			$data['total_user'] = $this->user->get_total_user();
			$data['today_user'] = $this->user->get_today_user();
			
			$data['statistic_test'] = $this->test->get_statistic_test();
			$data['statistic_user'] = $this->user->get_statistic_user();
			$data['statistic_question'] = $this->question->get_statistic_question();
			
			$content = $this->load->view('index', $data, true);
			$this->load->view('template', array('content' => $content));
		}
		else
		{
			//If no session, redirect to login page
			redirect('login', 'refresh');
		}
	}
	
	function question($page = 'home', $ques_id = '0')
	{
		if($this->session->userdata('logged_in'))
		{
			$session_data = $this->session->userdata('logged_in');
			$data['username'] = $session_data['username'];
			$result = $this->user->get_user($data['username']);
			if ($result)
			{
				$data['full_name'] = $result->full_name;
				$data['last_login'] = $result->last_login;
				$data['is_admin'] = $result->is_admin;
			}
			
			$options = $this->category->get_category();
			
			$data['dropdown'] = form_dropdown('category', $options, '1');
			
			$data['flash_error'] = "";
			$data['flash_ok'] = "";
			
			if ($this->session->flashdata('error')) {
				$data['flash_error'] = $this->session->flashdata('error');
			}
			if ($this->session->flashdata('ok')) {
				$data['flash_ok'] = $this->session->flashdata('ok');
			}
			
			if ($ques_id != '0') {
				$rs = $this->question->get_question($ques_id);
				if ($rs) {
					$data['question_id'] = $ques_id;
					$data['content'] = $rs->content;
					$data['level'] = $rs->level;
					$data['right_answer'] = $rs->right_answer;
					$data['note'] = $rs->note;
					$data['dropdown'] = form_dropdown('category', $options, $rs->category_id);
					
					$ans_rs = $this->answer->get_answer($ques_id);
					if ($ans_rs) {
						$data['ans1_id'] = $ans_rs[0]->answer_id;	
						$data['ans2_id'] = $ans_rs[1]->answer_id;	
						$data['ans3_id'] = $ans_rs[2]->answer_id;	
						$data['ans4_id'] = $ans_rs[3]->answer_id;	
						$data['ans1'] = $ans_rs[0]->content;	
						$data['ans2'] = $ans_rs[1]->content;	
						$data['ans3'] = $ans_rs[2]->content;	
						$data['ans4'] = $ans_rs[3]->content;	
					}
				}
			}
			
			$content = $this->load->view('question/'.$page, $data, true);
			$this->load->view('template', array('content' => $content));
		}
		else
		{
			//If no session, redirect to login page
			redirect('login', 'refresh');
		}
	}
	
	function test($page = 'home', $test_id = '0')
	{
		if($this->session->userdata('logged_in'))
		{
			$session_data = $this->session->userdata('logged_in');
			$data['username'] = $session_data['username'];
			$result = $this->user->get_user($data['username']);
			if ($result)
			{
				$data['full_name'] = $result->full_name;
				$data['last_login'] = $result->last_login;
				$data['is_admin'] = $result->is_admin;
			}
			
			$data['flash_error'] = "";
			$data['flash_ok'] = "";
			
			if ($this->session->flashdata('error')) {
				$data['flash_error'] = $this->session->flashdata('error');
			}
			if ($this->session->flashdata('ok')) {
				$data['flash_ok'] = $this->session->flashdata('ok');
			}
			
			if ($page == 'addnew')
			{
				$config = $this->myconfig->get_config();
				if ($config)
				{
					$data['default_max_score'] = $config->default_max_score;
					$data['default_num_question'] = $config->default_num_question;
					$data['default_time'] = $config->default_time;
				}
			}
			
			if ($page == 'view' && $test_id != '0')
			{
				$test_info = $this->test->get_test($test_id);	
				if ($test_info)
				{
					$data['test_id'] = $test_id;
					$data['test_name'] = $test_info->test_name;
					$data['time'] = $test_info->time;
					$data['num_question'] = $test_info->num_question;
					$data['max_score'] = $test_info->max_score;
					$data['created_user'] = $test_info->created_user;
					$data['created_date'] = $test_info->created_date;
					
					// get question
					$arr_question = array();
					$question_list_id = $this->question->get_question_by_test($test_id);
					if ($question_list_id)
					{
						foreach ($question_list_id as $row)
						{
							$question = $this->question->get_question($row->question_id);
							$ans_order = $row->answer_order;
							$ans_rs = $this->answer->get_answer($row->question_id);
							if ($question && $ans_rs)
							{
								$arr_info['content'] = $question->content;
								$arr_info['right_answer'] = $question->right_answer;
								$arr_info['ans1'] = $ans_rs[$ans_order[0] - 1]->content;
								$arr_info['ans2'] = $ans_rs[$ans_order[1] - 1]->content;
								$arr_info['ans3'] = $ans_rs[$ans_order[2] - 1]->content;
								$arr_info['ans4'] = $ans_rs[$ans_order[3] - 1]->content;
								array_push($arr_question, $arr_info);
							}
						}	
					}
					$data['arr_question'] = $arr_question;	
				}
			}
			
			if ($page == 'edit' && $test_id != '0')
			{
				$test_info = $this->test->get_test($test_id);	
				if ($test_info)
				{
					$data['test_id'] = $test_id;
					$data['test_name'] = $test_info->test_name;
					$data['time'] = $test_info->time;
					$data['level'] = $test_info->level;
					$data['num_question'] = $test_info->num_question;
					$data['max_score'] = $test_info->max_score;
					$data['note'] = $test_info->note;
					$data['created_user'] = $test_info->created_user;
					$data['created_date'] = $test_info->created_date;
				}
			}
			
			$content = $this->load->view('test/'.$page, $data, true);
			$this->load->view('template', array('content' => $content));
		}
		else
		{
			//If no session, redirect to login page
			redirect('login', 'refresh');
		}
	}
	
	function user($page = 'home')
	{
		if($this->session->userdata('logged_in'))
		{
			$session_data = $this->session->userdata('logged_in');
			$data['username'] = $session_data['username'];
			$result = $this->user->get_user($data['username']);
			if ($result)
			{
				// Check whether user is admin or not, if not redirect to home page
				if ($result->is_admin != 1 && $page != 'edit')
				{
					redirect('tdc', 'refresh');
				}
				
				$data['full_name'] = $result->full_name;
				$data['email'] = $result->email;
				$data['last_login'] = $result->last_login;
				$data['is_admin'] = $result->is_admin;
				$data['flash_error'] = "";
				$data['flash_ok'] = "";
				
				if ($this->session->flashdata('error')) {
					$data['flash_error'] = $this->session->flashdata('error');
				}
				if ($this->session->flashdata('ok')) {
					$data['flash_ok'] = $this->session->flashdata('ok');
				}
			}
			
			$content = $this->load->view('user/'.$page, $data, true);
			$this->load->view('template', array('content' => $content));
		}
		else
		{
			//If no session, redirect to login page
			redirect('login', 'refresh');
		}
	}
	
	function config($page = 'home')
	{
		if($this->session->userdata('logged_in'))
		{
			$session_data = $this->session->userdata('logged_in');
			$data['username'] = $session_data['username'];
			$result = $this->user->get_user($data['username']);
			$config = $this->myconfig->get_config();
			if ($result && $config)
			{
				$data['full_name'] = $result->full_name;
				$data['last_login'] = $result->last_login;
				$data['is_admin'] = $result->is_admin;
				$data['default_max_score'] = $config->default_max_score;
				$data['default_num_question'] = $config->default_num_question;
				$data['default_time'] = $config->default_time;
				$data['modified_user'] = $config->modified_user;
				$data['modified_date'] = $config->modified_date;
			}
			
			$data['flash_error'] = "";
			$data['flash_ok'] = "";
			
			if ($this->session->flashdata('error')) {
				$data['flash_error'] = $this->session->flashdata('error');
			}
			if ($this->session->flashdata('ok')) {
				$data['flash_ok'] = $this->session->flashdata('ok');
			}
			
			$content = $this->load->view('config/'.$page, $data, true);
			$this->load->view('template', array('content' => $content));
		}
		else
		{
			//If no session, redirect to login page
			redirect('login', 'refresh');
		}
	}

	function logout()
	{
		if($this->session->userdata('logged_in'))
		{
			$session_data = $this->session->userdata('logged_in');
			// set last login time
			$this->user->update_last_login($session_data['user_id'], $session_data['current_login_time']);
		}
		$this->session->unset_userdata('logged_in');
		session_destroy();
		redirect('login', 'refresh');
	}

}

?>